/**
 * Aistarfish.com Inc.
 * Copyright (c) 2017-2019 All Rights Reserved.
 */
package com.xf.dao;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.xf.model.App;
import com.xf.model.PageParam;

/**
 *
 * @author xiangfeng
 * Created by on 2019-12-17 20:58
 */
public class AppDao {

    private static App dao = new App().dao();

    public List<App> getAllPus() {
        return dao.find("select id,name,logo,ico,redirect_url,type from app where valid=1 and open=1 order by `order_code`");
    }

    public void addApp(App app) {
        app.setOrderCode(getSort(app.getType()));
        app.save();
    }

    private Integer getSort(Integer type){
        App first = dao.findFirst("select max(order_code) order_code from app where type = ?", type);
        if(first == null){
            return 1;
        }
        return first.getOrderCode()+1;
    }
    public List<App> getAll() {
        return dao.findAll();
    }

    public void update(App app) {
        app.update();
    }

    public void del(Integer id) {
        Db.deleteById("app",id);
    }

    public Page<App> getAppByPage(PageParam pageParam) {
        return dao.paginate(pageParam.getPageIndex(), pageParam.getPageSize(), "select id,name,logo,ico,redirect_url,open,type ",
                "from app where valid=1 order by order_code");
    }
}
